package org.jgs1904.quickstart.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author: 默苍璃
 * @date: 2025-06-0515:15
 */
@Service
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    @Lazy
    private PasswordEncoder pw;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.查询数据库判断用户名是否存在，如果不存在抛出 UsernameNotFoundException异常
        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("用户名不存在");
        }

        // 2.把查询出来的密码（注册时已经加密过）进行解析，或直接把密码放入构造方法中
        String password = pw.encode("123");
        // return new User(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList("/main.html,admin,normal,ROLE_abc"));
        return new User(username, password, AuthorityUtils.commaSeparatedStringToAuthorityList("/main.html,admin,ROLE_abc,/insert,/delete"));
    }

}
